{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 非稳态通常除霜周期时间计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pandas import DataFrame,Series\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "filename='data/mine/2017年07月29日 10時04分30秒.csv'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 输入 MAX 频率\n",
    "MAX频率\n",
    "\n",
    "除霜频率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "max_hz=99\n",
    "defrost_hz=99"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## csv文件转码为utf-8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# import csv\n",
    "# csvfile=open(filename,encoding='CP932')\n",
    "# newname=filename.replace('秒','秒_new')\n",
    "# newfile=open(newname,'w+',newline='\\n',encoding='UTF-8')\n",
    "# reader=csv.reader(csvfile)\n",
    "# writer=csv.writer(newfile)\n",
    "\n",
    "# for line in reader:\n",
    "#     writer.writerow(line)\n",
    "\n",
    "# csvfile.close()\n",
    "# newfile.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data=pd.read_csv(filename,encoding='CP932')\n",
    "# data.to_csv(filename[:-4]+'_new.csv',encoding='GBK',index=False) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 插入时间序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "seconds=np.arange(0,5*data.shape[0],5)\n",
    "minutes=seconds/60"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data.insert(0,'seconds',seconds)\n",
    "data.insert(0,'minutes',minutes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 目标频率中间跳 0 的处理\n",
    "在剔除目标频率为0的数据前插入时间序列。\n",
    "\n",
    "剔除目标频率为0的数据后，重新索引。\n",
    "\n",
    "避免中间中目标频率异常跳0引起的index间断，避免中间跳0的时间段被忽略。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "comp_on=data[data['COMP  運転周波数(目標)']>0]\n",
    "comp_on.index=np.arange(0,comp_on.shape[0],1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 异常值处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 异常值显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "outlier_mask=data['COMP  運転周波数(目標)']>max_hz\n",
    "if outlier_mask.any():         #存在异常值\n",
    "    outlier_index=data[outlier_mask].index\n",
    "    print(data.loc[outlier_index,'COMP  運転周波数(目標)'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 异常值修正"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "if outlier_mask.any():         #存在异常值\n",
    "    outlier=data['COMP  運転周波数(目標)'][outlier_index].values\n",
    "    outlier_fix=(data['COMP  運転周波数(目標)'][outlier_index-1].values+data['COMP  運転周波数(目標)'][outlier_index+1].values)/2\n",
    "    data.set_value(outlier_index,'COMP  運転周波数(目標)',outlier_fix)\n",
    "#     print('存在异常值，位置:',outlier_index, outlier)\n",
    "#     print('异常值已修正为:',outlier_fix) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 修正后的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "if outlier_mask.any():\n",
    "    print(data.loc[outlier_index,'COMP  運転周波数(目標)'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取起止时间点 `index`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "hz_mask=comp_on['COMP  運転周波数(目標)']>(max_hz-1)   #考虑存在保护的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "start_max=[]\n",
    "end_max=[]\n",
    "for i in hz_mask.index[:-1]:\n",
    "    if not hz_mask[i] and hz_mask[i+1]:\n",
    "        start_max.append(i+1)\n",
    "    elif hz_mask[i] and not hz_mask[i+1]:\n",
    "        end_max.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# columns=[['第一周期','第一周期','第二周期','第二周期','第三周期','第三周期','第四周期','第四周期','第五周期','第五周期'],\n",
    "#         ['不动作','除霜','不动作','除霜','不动作','除霜','不动作','除霜','不动作','除霜']]\n",
    "# index=['2017/7/20']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([51, 542, 574, 1015, 1048, 1488, 1520, 1960, 1993, 2432, 2464],\n",
       " [526, 558, 998, 1032, 1471, 1505, 1943, 1977, 2415, 2448])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start_max,end_max"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算周期长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "phase_non_def={}\n",
    "i=1\n",
    "for m in np.arange(0,9,2):\n",
    "    if m==0:\n",
    "        phase_non_def[i]=comp_on['minutes'][end_max[m]] - comp_on['minutes'][0]\n",
    "    else:\n",
    "        phase_non_def[i]=comp_on['minutes'][end_max[m]] -comp_on['minutes'][start_max[m]] \n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "phase_def={}\n",
    "i=1\n",
    "for m in np.arange(1,10,2):\n",
    "    phase_def[i]=comp_on['seconds'][end_max[m]]-comp_on['seconds'][start_max[m]]\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "format=lambda x: '%.2f' %x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>phase_non_def</th>\n",
       "      <td>43.83</td>\n",
       "      <td>35.33</td>\n",
       "      <td>35.25</td>\n",
       "      <td>35.25</td>\n",
       "      <td>35.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>phase_def</th>\n",
       "      <td>80</td>\n",
       "      <td>85</td>\n",
       "      <td>85</td>\n",
       "      <td>85</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   1      2      3      4      5\n",
       "phase_non_def  43.83  35.33  35.25  35.25  35.17\n",
       "phase_def         80     85     85     85     80"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df=DataFrame({'phase_def':phase_def,'phase_non_def':phase_non_def},columns=['phase_non_def','phase_def'])\n",
    "df['phase_non_def']=df['phase_non_def'].apply(format)\n",
    "# df.unstack().swaplevel(1,0).sort_index(level=0)\n",
    "# df.T.to_csv('data/mine/defrost_out.csv')\n",
    "df.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 平均除霜温"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ss=comp_on['室外　霜取ＴＨ　温度'][120:181]\n",
    "Tsimo0=np.where(ss>200,ss-2**8,ss).mean()\n",
    "Tsimo0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Bugs\n",
    "\n",
    "1. 中间跳出小于 `max_hz` 的频率；\n",
    "2. 除霜频率 不等于 最大频率；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
